package com.mayisoftware.mayioa.business.score.impl.serviceImpl;

import com.mayisoftware.mayioa.business.score.api.domain.ScoreInfo;
import com.mayisoftware.mayioa.business.score.api.service.IScoreInfoService;
import com.mayisoftware.mayioa.business.score.impl.dao.ScoreInfoMapper;
import com.mayisoftware.mayioa.common.impl.support.Convert;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@Service
public class ScoreInfoServiceImpl implements IScoreInfoService {

    @Autowired
    ScoreInfoMapper scoreInfoMapper ;

    @Override
    public List<Map<String, String>> getPlanSelectList(Long userId) {
        return scoreInfoMapper.selectPlanSelectList(userId);
    }

    @Override
    public List<Map<String,Object>> getSignPlanUsers(Long planId) {
        return scoreInfoMapper.selectSignPlanUsers(planId);
    }

    @Override
    public List<Map<String, Object>> selectPlanDetails(Long planId) {
        return scoreInfoMapper.selectPlanDetails(planId);
    }

    @Override
    public List<Map<String, Object>> selectPlanDetailsScore(Long planId,Long userId) {
        return scoreInfoMapper.selectPlanDetailsScore(planId,userId);
    }

    @Override
    public List<ScoreInfo> selectScoreInfoListByUserAndPlan(ScoreInfo scoreInfo) {
        return scoreInfoMapper.selectScoreInfoListByUserAndPlan(scoreInfo);
    }

    @Override
    public int updateScoreInfo(ScoreInfo scoreInfo) {
        scoreInfoMapper.deleteByPlanIdAndUserId(scoreInfo);
        int cnt = this.insertScoInfo(scoreInfo);
        return cnt;
    }

    @Override
    public int insertScoInfo(ScoreInfo scoreInfo) {
        String[] planItemIds = scoreInfo.getPlanItemIds().split(",");
        String[] planItemScores = scoreInfo.getPlanItemScores().split(",");
        String[] planItemsReasons = scoreInfo.getPlanItemsReasons().split(",");
        int s = 0;
        for(int i=0;i<planItemIds.length;i++){
            if(!StringUtils.isEmpty(planItemIds[i])){
                scoreInfo.setScorePlanItemId(Integer.parseInt(planItemIds[i]));
                scoreInfo.setScoreInfo(BigDecimal.valueOf(Double.parseDouble(planItemScores[i])));
                if(planItemsReasons.length>i){
                    scoreInfo.setScoreRemark(planItemsReasons[i]);
                }
                if(scoreInfoMapper.isExistsScoreInfo(scoreInfo)>0){
                    return -1 ;
                }
                s += scoreInfoMapper.insertSelective(scoreInfo);
            }
        }
        return s;
    }

    @Override
    public int insertOneScoInfo(ScoreInfo scoreInfo) {
        return scoreInfoMapper.insertSelective(scoreInfo);
    }

    @Override
    public List<ScoreInfo> selectScoreInfoList(ScoreInfo scoreInfo) {
        return selectScoreInfoList(scoreInfo,null);
    }

    public List<ScoreInfo> selectScoreInfoList(ScoreInfo scoreInfo,Long loginUserId) {
        return scoreInfoMapper.selectScoreInfoList(scoreInfo, loginUserId);
    }

    @Override
    public int deleteScoreInfoByIds(String ids) {
        Long[] recordIds = Convert.toLongArray(ids);
        return scoreInfoMapper.deleteScoreInfoByIds(recordIds);
    }

    @Override
    public ScoreInfo selectByPrimaryKey(Long recordId) {
        return scoreInfoMapper.selectByPrimaryKey (recordId.intValue());
    }

    public int updateByPrimaryKeySelective(ScoreInfo scoreInfo){
        return scoreInfoMapper.updateByPrimaryKeySelective(scoreInfo);
    }


}
